
package de.hpk.jav;

import org.stringtemplate.v4.misc.Aggregate;

import com.sun.jndi.toolkit.ctx.AtomicContext;

import de.hpk.antlar.HelloBaseVisitor;
import de.hpk.antlar.HelloParser.AtomContext;
import de.hpk.antlar.HelloParser.DifContext;
import de.hpk.antlar.HelloParser.MinusContext;
import de.hpk.antlar.HelloParser.MultContext;
import de.hpk.antlar.HelloParser.OperationContext;
import de.hpk.antlar.HelloParser.PlusContext;

public class MyVisitor extends HelloBaseVisitor<String> {
	
	@Override
	protected String aggregateResult(String aggregate, String nextResult) {
		if (aggregate== null)
			return nextResult;
		if (nextResult == null)
			return aggregate;
		else 
			return aggregate;
	}
	
	@Override
	public String visitAtom(AtomContext ctx){
		double atom  = Double.parseDouble(ctx.getText());
		return (String.valueOf(atom));
	}
	
	// ÄNDERN!!! TYP SICHER! INTEGER FLOAT, VARIABLEN!!!!
	@Override
	public String visitPlus(PlusContext ctx){	
		String test = visitChildren(ctx);
		System.out.println("VISITCHILDREN: "  + test + "\tLeft : "+ ctx.left.getText() + "\tRight: "+ ctx.right.getText());
		double Number1  = Double.parseDouble(test);
		double Number2  = Double.parseDouble(ctx.right.getText());
		double result = Number1 + Number2;
		System.out.println("Result: "  + result);
		return String.valueOf(result); 
	}
	
	
	@Override
	public String visitMinus(MinusContext ctx){	
		double left_number   = Double.parseDouble(visitChildren(ctx.left));
		double right_number  = Double.parseDouble(ctx.right.getText());
		return (String.valueOf(left_number - right_number));
	}
	
	@Override
	public String visitMult(MultContext ctx){
		String test = visitChildren(ctx);
		System.out.println("VISITCHILDREN: "  + test + "\tLeft : "+ ctx.left.getText() + "\tRight: "+ ctx.right.getText());
		double Number1  = Double.parseDouble(test);
		double Number2  = Double.parseDouble(ctx.right.getText());
		double result = Number1 * Number2;
		System.out.println("Result: "  + result);
		return String.valueOf(result); 
	}
	
	
//	// ÄNDERN!!! TYP SICHER! INTEGER FLOAT, VARIABLEN!!!!
//	@Override
//	public String visitMinus(MinusContext ctx){
//		if (ctx.left.getText().contains("-")){
//			String left_string  = visitMinus( (MinusContext) ctx.left);
//			double left_number  = Double.parseDouble(left_string);
//			double right_number = Double.parseDouble(ctx.right.getText());
//			return (String.valueOf(left_number - right_number));
//			}
//		double left_number   = Double.parseDouble(ctx.left.getText());
//		double right_number  = Double.parseDouble(ctx.right.getText());
//		return (String.valueOf(left_number - right_number));
//	}
//	
//	public String visitMult(MultContext ctx){
//		if (ctx.left.getText().contains("*")){
//			String left_string = visitMult( (MultContext) ctx.left);
//			double left_number  = Double.parseDouble(left_string);
//			double right_number  = Double.parseDouble(ctx.right.getText());
//			return (String.valueOf(left_number * right_number));
//			}
//		double left_number   = Double.parseDouble(ctx.left.getText());
//		double right_number  = Double.parseDouble(ctx.right.getText());
//		return (String.valueOf(left_number * right_number));
//	}
//	
//	public String visitDif(DifContext ctx){
//		if (ctx.left.getText().contains("/")){
//			String left_string  = visitDif( (DifContext) ctx.left);
//			double left_number  = Double.parseDouble(left_string);
//			double right_number = Double.parseDouble(ctx.right.getText());
//			if (right_number == 0)
//// ERROR_CODE HINZUFÜGEN!!!!				
//				return "DIFFISION WITH NULL!";
//			return (String.valueOf(left_number / right_number));
//			}
//		double left_number   = Double.parseDouble(ctx.left.getText());
//		double right_number  = Double.parseDouble(ctx.right.getText());
//		if (right_number == 0)
//			return "ERROR: DIFFISION WITH NULL!";
//		return (String.valueOf(left_number / right_number));
//	}
}

